home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / graphio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  2.7 KB  |  126 lines

  1. /* Convert between LISE spectra files and Cricket Graph Data
  2. */
  3.  
  4. #include <stdio.h>
  5. #include <spec.h>
  6.  
  7. float x,y,*spc,*err,*tim;
  8.  
  9. help()
  10. {
  11.   printf("graphio spectrum [-l2g] [-g2l]\n");
  12.   printf("  converts spectra from LISE to CRICKETGRAPH and vice versa\n");
  13.   printf("  options:\n");
  14.   printf("     -l2g     convert from LISE to CRICKETGRAPH\n");
  15.   printf("     -g2l     convert from CRICKETGRAPH to LISE\n");
  16. }
  17.  
  18. main(argc,argv)
  19. int argc;
  20. char *argv[];
  21. {
  22. int n,m,i,max;
  23. char z[80],comment[80];
  24.  
  25.  
  26.    spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  27.    err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  28.    tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  29.    if(tim==NULL) {
  30.       printf("sorry, not enough memory\n");
  31.       exit(-1);
  32.    }
  33.  
  34.    if(checkopt(argc,argv,"-l2g",z)) {
  35.       max=readspec(argv[1],spc,err,tim,comment);
  36.       strcpy(z,argv[1]); n = strlen(z);
  37.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  38.       writeplot(z,spc,err,tim,max,comment);
  39.    }
  40.  
  41.    if(checkopt(argc,argv,"-g2l",z)) {
  42.       max = readplot(argv[1],spc,err,tim,comment);
  43.       strcpy(z,argv[1]); n = strlen(z);
  44.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  45.       writespec(z,spc,err,max,2,comment);
  46.       strcat(z,".tim");
  47.       writespec(z,tim,err,max,2,comment);
  48.    }
  49.    free(err); free(spc); free(tim);
  50.    exit(0);
  51. }
  52.  
  53. writeplot(name,spc,err,tim,max,comment)
  54. char *name, *comment;
  55. float *spc, *err, *tim;
  56. int max;
  57. {
  58. FILE *fp;
  59. int i,n,m;
  60. char *s;
  61.  
  62.    s = (char *) malloc(256);
  63.  
  64.    sprintf(s,"%s.dat",name);
  65.    fp = fopen(s,"w");
  66.    if(fp == NULL) {
  67.       fprintf(stderr,"graphio: could not open >%s< for write\n",s);
  68.       free(s);
  69.       return(0);
  70.    }
  71. /*
  72.    fprintf(fp,"*TITLE* %s\n*XLABEL* Time\n*LEGEND* %s\n",comment,name);
  73. */
  74.    for(n = 0; n < max; n++) {
  75.       fprintf(fp,"%f\x09%f\x09%f\n",tim[n],spc[n],err[n]);
  76.    }
  77.    fclose(fp);
  78.  
  79.    free(s);
  80. }
  81.  
  82. readplot(name,spc,err,tim,comment)
  83. char *name, *comment;
  84. float *spc, *err, *tim;
  85. {
  86. FILE *fp;
  87. int i,n,max;
  88. char *s, *z;
  89.  
  90.    s = (char *) malloc(256); z = (char *) malloc(256);
  91.  
  92.    strcpy(s,name);
  93.    fp = fopen(s,"r");
  94.    if(fp == NULL) {
  95.       strcat(s,".dat");
  96.       fp = fopen(s,"r");
  97.       if(fp == NULL) {
  98.          fprintf(stderr,"graphio: could not open >%s< for read\n",name);
  99.          free(s); free(z);
  100.          return(0);
  101.       }
  102.    }
  103. /*
  104.    while(!feof(fp)) {
  105.       fgets(s,200,fp); sscanf(s,"%s",z);
  106.       if(strcmp(z,"*TITLE*") == 0) {
  107.          n = strlen(z); i = 0; while(s[n] != 0) comment[i++] = s[n++];
  108.          comment[i] = 0;
  109.       }
  110.       if(strcmp(z,"*LEGEND*") == 0) break;
  111.    }
  112. */
  113.    max = 0;
  114.    while(!feof(fp)) {
  115.       fgets(s,200,fp);
  116.       if(s[0] == '*') continue;
  117.       if(strlen(s) < 1) break;
  118.       sscanf(s,"%f %f %f\n",&tim[max],&spc[max],&err[max]);
  119.       max = max + 1;
  120.    }
  121.    fclose(fp);
  122.  
  123.    free(s); free(z);
  124. }
  125.  
  126.